﻿using Cxuu.Common;
using Cxuu.Models;
using Cxuu.Service;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using PetaPoco;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Cxuu.Web.Admin
{
    [Area("Admin")]
    [Authorize]
    public class NoticeController : Base
    {
        private readonly NoticeKeyService _notice;
        private readonly ConfigService _configService;
        private readonly AttmentService _attment;
        private readonly AdminUserService _user;
        private readonly LogService _logService;
        public NoticeController(AdminUserService adminUserService, LogService logService, AttmentService attmentService, ConfigService configService, NoticeKeyService noticeKeyService)
        {
            _notice = noticeKeyService;
            _user = adminUserService;
            _configService = configService;
            _logService = logService;
            _attment = attmentService;
        }

        [HttpGet]
        public async Task<IActionResult> IndexAsync(int? readList, string keyword, int? status, int page = 1, int limit = 15, int json = 0, int geted = 0)
        {
            if (json == 1)
            {
                ViewAdminUser adminUser = await UserClaims();
                Page<ViewNotice> list;
                if (readList == 1)
                    list = await _notice.GetPageListAsync(keyword, page, limit, adminUser.Id, 0, geted);
                else
                    list = await _notice.GetPageListAsync(keyword, page, limit, adminUser.Gid == 1 ? 0 : adminUser.Id, status, geted);
                return Json(new { code = 0, data = list.Items, count = list.TotalItems });
            }
            if (readList is null) return View();
            else return View("ReadList");
        }

        [HttpGet, ActionName("Read")]
        public async Task<IActionResult> ReadAsync(int id, int noticeId)
        {
            Notice notice = await _notice.ReadAsync(id, noticeId);
            if (notice == null) return View();
            List<ViewAttment> attments = null;
            if (!string.IsNullOrEmpty(notice.Att))
                attments = await _attment.GetListAsync(notice.Att);
            ViewBag.attments = attments;
            return View(notice);
        }

        public async Task<IActionResult> AddOrEditAsync(int jsonUser)
        {
            if (jsonUser == 1)
            {
                List<UserIdAndName> adminUsers = await _user.GetUserIdAndNameListAsync();
                return Json(new { code = 0, data = adminUsers });
            }
            //附件信息
            EditConfigSiteInfo configSiteInfo = await _configService.GetByNameAsync();
            ViewBag.fileExts = configSiteInfo.FileExt;
            ViewBag.fileSize = configSiteInfo.FileSize;

            ViewAdminUser adminUser = await UserClaims();
            ViewBag.uid = adminUser.Id;
            return View();
        }

        [HttpPost, ActionName("CreatePost")]
        [XssFieldFilter]//防Xss
        public async Task<IActionResult> CreatePostAsync(EditNotice notice)
        {
            if (ModelState.IsValid)
            {
                ViewAdminUser user = await UserClaims();
                await _notice.InsertAsync(notice, user.Id);
                await _logService.InsertLogSqlAsync(await UserInfoAsync(), "发布站内信");
                return Json(new { status = 1, msg = "站内信发布成功！" });
            }
            else
            {
                var error = ModelState.GetErrorMessages();
                return Json(new { status = 0, msg = error });
            }
        }

        [HttpPost]
        public async Task<IActionResult> Delete(int id)
        {
            if (await _notice.DeleteByPriKeyAsync(id) > 0)
            {
                await _logService.InsertLogSqlAsync(await UserInfoAsync(), "删除通知 id:" + id);
                return Json(new { status = 1, msg = "删除成功！" });
            }

            else
                return Json(new { status = 0, msg = "删除失败！" });
        }

    }
}
